﻿using Microsoft.EntityFrameworkCore;
using MyServer.Todo.Domain;

namespace MyServer.Todo.Infrastructure.Repositories
{
    public abstract class Repository<TEntity> : IRepository<TEntity> where TEntity : Entity, IAggregateRoot
    {
        protected readonly TodoDbContext _dbContext;
        public Repository(TodoDbContext dbContext)
        {
            _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
        }
        public IUnitOfWork UnitOfWork => _dbContext;

        public abstract TEntity Get(string id);

        public virtual TEntity Add(TEntity entity)
        {
            return _dbContext.Set<TEntity>().Add(entity).Entity;
        }

        public virtual void Update(TEntity entity)
        {
            _dbContext.Entry(entity).State = EntityState.Modified;
        }
    }
}
